//
//  HMTabbar.swift
//  swift一遍敲
//
//  Created by 刘耀宗 on 16/4/19.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

import UIKit

class HMTabbar: UITabBar {
    var composeClosure: ( () -> () )?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    private func setFrame()
    {
        
        var arr = self.subviews
        let obj = arr.last
        print("obj = \(obj)")
        arr.removeFirst()
        arr.removeLast()
        let count:Int = 5
        let Width: CGFloat = UIScreen.mainScreen().bounds.size.width / CGFloat(count)
        print("width= \(Width)")
        var index: Int = 0
       self.addBtn.frame = CGRectMake(2*Width, 0, Width, self.frame.size.height)
        var  X:CGFloat = 0
        //循环
        for view in arr {
            
            if view.isKindOfClass(UIButton) {
                print("返回了")
                return
            }
            else
            {
               //设置item
                view.frame =  CGRectMake(X, 0, Width, view.frame.size.height)
                
                if index==1 {
                    X += 2*Width
                }
                else
                {
                    X +=  Width
                }
                  index += 1
                }
        }
    
    
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
           setFrame()
    }
    //懒加载一个btn
    lazy var addBtn: UIButton = {
     let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        //添加单击事件
        btn.addTarget(self, action: #selector(HMTabbar.BTnClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        //创建一个button
        self.addSubview(btn)
        
        return btn
    
    
    
    }()
   @objc private func BTnClick()
    {
        //调用闭包
        composeClosure!()
    
    
    }
 
    
    //定义一个闭包
 

}
